/**
 * 
 */
package neptune.web.client.view.components.editor;

import neptune.web.client.model.topology.SerializableLink;
import neptune.web.client.model.topology.SerializableTopology;
import neptune.web.client.utils.TopologyGenerationUtils;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.TreeItem;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class AddLinkCommand implements Command {

	private SerializableTopology topology;
	private TreeItem item;
	private final TopologyEditor editor;

	public AddLinkCommand(SerializableTopology topology, TreeItem treeItem,
			TopologyEditor editor) {
		this.topology = topology;
		this.item = treeItem;
		this.editor = editor;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.Command#execute()
	 */
	public void execute() {
		final SerializableLink link = new SerializableLink();
		link.setId(TopologyGenerationUtils.generatelINKName(topology));
		link.setFullDuplex(true);
		topology.getLinks().add(link);

		final TreeItem linkItem = new TreeItem();
		linkItem.setWidget(new LinkMenuBar(link, topology, linkItem, editor));
		item.addItem(linkItem);

		LinkPropsDialog dialog = new LinkPropsDialog(link, topology, linkItem,
				editor);
		dialog.setCancelCallback(new CancelCallBack() {
			public void onCancel() {
				linkItem.remove();
				topology.getLinks().remove(link);
			}
		});
		dialog.center();
	}

}
